package com.aube.plugin.gallery.preview;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.volley.toolbox.Scheme;
import com.huyn.baseframework.dynamicload.DynamicLoaderHelper;
import com.huyn.baseframework.utils.Utils;
import com.ryg.dynamicload.internal.DLPluginPackage;

import java.util.List;

/**
 * Created by huyn on 2015/12/29.
 */
public class ImgPreviewVPAdapter extends PagerAdapter {

    private int originIndex = 0;
    private Bitmap originBitmap;
    private boolean isChanged = false;
    private List<String> mResources;
    private ViewPager mImgBig;

    private IPreviewPresenter iPreviewPresenter;

    private DLPluginPackage dlPluginPackage;
    private Context mContext;

    public ImgPreviewVPAdapter(Context context, DLPluginPackage dlPluginPackage, ViewPager vp, List<String> resources, Bitmap bitmap) {
        originBitmap = bitmap;
        this.mContext = context;
        this.dlPluginPackage = dlPluginPackage;
        this.mResources = resources;
        this.mImgBig = vp;
    }

    public interface IPreviewPresenter {
        public void savePic(String url);
        public void showSavePicDialog();
        public void animOut();
        public boolean canLongClick();
    }

    public void append(List<String> resList) {
        if(mResources != null) {
            mResources.addAll(resList);
            notifyDataSetChanged();
        }
    }

    public void setiPreviewPresenter(IPreviewPresenter presenter) {
        this.iPreviewPresenter = presenter;
    }

    public void setOriginItem(int index) {
        originIndex = index;
    }

    public Boolean isPagerChanged() {
        if (!isChanged) {
            isChanged = originIndex != mImgBig.getCurrentItem();
        }
        return isChanged;
    }

    public void setIsChanged(boolean changed) {
        isChanged = changed;
    }

    @Override
    public int getCount() {
        return mResources == null ? 0 : mResources.size();
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    public void savePicture() {
        if(iPreviewPresenter != null)
            iPreviewPresenter.savePic(mResources.get(mImgBig.getCurrentItem()));
    }

    public String getCurrentUrl() {
        return mResources.get(mImgBig.getCurrentItem());
    }

    public List<String> getResources() {
        return mResources;
    }

    public void setResources(List<String> resources) {
        mResources = resources;
        notifyDataSetChanged();
    }

    public void removeCurrentItem(){
        originBitmap = null;
        mResources.remove(mImgBig.getCurrentItem());
        notifyDataSetChanged();
    }
    private View.OnLongClickListener bigImageLongClick = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if(iPreviewPresenter == null)
                return false;
            if (iPreviewPresenter.canLongClick()) {
                iPreviewPresenter.showSavePicDialog();
            }
            return true;
        }
    };

    //点击图片，返回上一页
    public View.OnClickListener imgClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(iPreviewPresenter != null)
                iPreviewPresenter.animOut();
        }
    };

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
//        boolean hasPreview = false;
//        if (position == originIndex && originBitmap != null) {
//            hasPreview = true;
//        }

        String url = mResources.get(position);

        ViewPagerLayoutBase fragment = (ViewPagerLayout) DynamicLoaderHelper.loadTargetLayout(mContext, getDLPluginPackage(), ViewPagerLayout.class.getName());

        container.addView(fragment, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        fragment.setClickListener(imgClick, bigImageLongClick);
        fragment.setPreviewGetterAndIndex(position, new IPreiewGetter() {
            @Override
            public Bitmap getPreview() {
                if (position == originIndex && originBitmap != null)
                    return originBitmap;
                return null;
            }

            @Override
            public void updateRes(String url, int index) {
                Utils.sysout("replace resource at:" + index + " by [" + url + "]");
                mResources.set(index, url);
            }
        });
        fragment.setAsset(url);

        return fragment;
    }

    private DLPluginPackage getDLPluginPackage() {
        return dlPluginPackage;
    }

    public interface IPreiewGetter {
        public Bitmap getPreview();
        public void updateRes(String url, int index);
    }

}
